from django.http import JsonResponse
from django.shortcuts import render, redirect
# Create your views here.
from django.views import View

from apps.user.models import JobInfo, User, Collection_job


class Index(View):
    def get(self, request):
        username = request.user.username
        if not username:
            return redirect('/login')
        print('---------', username)

        try:
            user = User.objects.get(username=username)
        except Exception as e:
            print(e)
            return JsonResponse({'code': 400, "errmsg": "数据库查询失败"})
        return render(request, 'admin/index.html', {'user': user})


class Home(View):
    def get(self, request):
        user_id = request.user.id
        try:
            user = User.objects.get(id=user_id)

            if user.is_admin == 1:
                users = User.objects.filter().all()
                return render(request, 'admin/user_list.html', {'users': users})
        except Exception as e:
            print(e)
        else:
            return render(request, 'admin/user_list.html')


class Showindex(View):
    def get(self, request):
        try:
            job_info = JobInfo.objects.all()

            print("------------", job_info)
        except Exception as e:
            job_info = JobInfo.objects.all()
            print(e)

        return render(request, 'admin/showindex.html', {'job_info': job_info})


# class User_list(View):
#     def get(self, request):
#         return render(request, 'system_user_list.html')
#
#
# class Menu_list(View):
#     def get(self, request):
#         return render(request, 'system_menu_list.html')


class Login(View):
    def get(self, request):
        return render(request, 'system_login.html')


class Register(View):
    def get(self, request):
        return render(request, 'register.html')


class JobInfos(View):
    def get(self, request):
        user_id = request.user.id
        print(user_id)
        try:
            user = User.objects.get(id=user_id)
            if user.is_admin == 1:
                jobs = JobInfo.objects.filter().all()
                print('------------------------------', jobs)
                return render(request, 'admin/news_edit.html', {'jobs': jobs})
        except Exception as e:
            print(e)
        else:
            return render(request, 'admin/news_edit.html')


class UserPassInfo(View):
    def get(self, request):
        return render(request, 'admin/user_pass_info.html')


class AddJob(View):
    def get(self, request):
        return render(request, 'admin/news_edit_detail.html')


class ChangeJob(View):
    def get(self, request, id):
        job = JobInfo.objects.get(id=id)
        return render(request, 'admin/jobchange.html', {'job': job})


class AddUser(View):
    def get(self, request):
        return render(request, 'admin/adduser.html')


class ChangeUser(View):
    def get(self, request, id):
        user = User.objects.get(id=id)
        return render(request, 'admin/changeuser.html', {'user': user})


class Detail(View):
    def get(self, request, id):
        user_id = request.user.id
        jobinfo = JobInfo.objects.get(id=id)

        # 计算当前用户是否收藏了这个篇文章
        try:
            job = Collection_job.objects.filter(user_collection_id=user_id, jobs_id=jobinfo.id)
            if job:
                jobinfo.collection = False
            else:
                jobinfo.collection = True
        except Exception as e:
            print(e)
            print('------------------------------------------------------')

        return render(request, 'admin/detail.html', {"jobinfo": jobinfo})

    # if user_id in news_collected_user_id:
    #     news.can_collect = False  # 已经收藏了，就不要在收藏了
    # else:
    #     news.can_collect = True  # 可以收藏

    # job_can = Collection_job.objects.get(jobs_id=id, user_collection_id=user_id)
    # if job_can:
    #     return render(request, 'admin/detail.html', {"jobinfo": jobinfo}, {'job_can': job_can})
    # else:


class Collection(View):
    def get(self, request):
        user_id = request.user.id
        job_info = []
        try:
            users = Collection_job.objects.filter(user_collection_id=user_id)

            for user in users:
                job_info.append(user.jobs)
        except Exception as e:
            print(e)
        return render(request, 'admin/collection.html', {'job_info': job_info})
